//
//  BaseNavigationController.swift
//  SwiiftProject
//
//  Created by 圣光大人 on 2019/5/13.
//  Copyright © 2019 lazyloading. All rights reserved.
//

import UIKit

class BaseNavigationController: UINavigationController,UINavigationControllerDelegate {
    
    var popDelegate: UIGestureRecognizerDelegate?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.delegate = self;
        self.popDelegate = self.interactivePopGestureRecognizer?.delegate
        
    }
    
    //    //MARK: 🐷 System delegate 🐷
    func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
        
        if viewController == self.viewControllers[0] {
            self.interactivePopGestureRecognizer!.delegate = self.popDelegate
        }else{
            self.interactivePopGestureRecognizer!.delegate = nil
        }
    }
    
    //MARK: 🐷 Override 🐷
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        
        if self.viewControllers.count > 0 {
            viewController.hidesBottomBarWhenPushed = true;
            let item = viewController.navigationItem;
            let back = UIButton.init(type: .custom);
            back.frame = CGRect.init(x: 0, y: 0, width: 44, height: 44);
            back.setImage(ZMImage("fanhui"), for: .normal);
            back.contentHorizontalAlignment = .left;
            back.addTarget(self, action: #selector(back(sender:)), for: .touchUpInside);
            item.leftBarButtonItem = UIBarButtonItem.init(customView: back);
            
        }
        super.pushViewController(viewController, animated: animated);
    }
    
    
    @objc func back(sender : UIButton) -> Void {
        self.popViewController(animated: true);
    }
    
    override var preferredStatusBarStyle: UIStatusBarStyle{
        return .lightContent;
    }
}
